/**
 * Created by Liujx on 2017-10-13 09:42:07
 */
const app = getApp();
const sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
const getBookListUrl = require('../../config').getBookListUrl;
const host = require('../../config').host;

Page({
    data: {
        host: host,
        inputVal: "",
        imgUrls: [
          'https://localhost/upload/banner1.jpg', //直接把轮播写死了
          'https://localhost/upload/banner2.jpg'  //直接把轮播写死了      
        ],
        tabs: ["课外朗读"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        page: 1,
        bookListItem: []
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    },
    onLoad: function () {
        var that = this;
        app.getUserInfo(function (userInfo) {
          //更新数据
          console.log(userInfo)
          that.setData({
            userInfo: userInfo
          });
          wx.setStorageSync('user_nickname', userInfo.nickName)
          wx.setStorageSync('user_head', userInfo.avatarUrl)
        })
        that.getBookListRequest();
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    tapBookList: function(e) {
        var BookListID = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../bookList/bookList?id=' + BookListID
        })
    },
    tapBook: function() {
        wx.navigateTo({
            url: '../bookDetails/bookDetails?id='
        })
    },
    getBookListRequest: function() {
        var that = this
        wx.showToast({
            title: '数据加载中',
            icon: 'loading',
            duration: 5000
        })
        wx.request({
            url: getBookListUrl,
            data: {
                "page": that.data.page
            },
            success: function(res) {
                wx.hideLoading();
                console.log(res)
                that.setData({
                    bookListItem: res.data.data.list
                });
            },
            fail: function({errMsg}) {
                console.log('request fail', errMsg)
                that.setData({
                    loading: false
                })
            }
        })
    },
    showLoading() {
      wx.showNavigationBarLoading();
      wx.showToast({
        title: "正在加载",
        icon: 'loading',
        duration: 1000
      })
      var that = this;
      that.data.page = that.data.page + 1;
      that.setData({
        hasMore: true,
      });
      wx.request({
        url: getBookListUrl,

        data: { page: that.data.page },  //设置一个page原始值，在原始page值上加1

        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          var list = res.data.data.list
          if (list.length != 0) {   //用到length，666，解决了page翻页问题
            that.setData({
              bookListItem: res.data.data.list,
              hidden: true,
              hasMore: false,
            });
          } else {
            wx.showToast({
              title: "没有了", //再次上翻回到首页
              icon: 'loading',
              duration: 1000
            })
            that.setData({
              page: 0,    //page重置，否则的话，再次下拉刷新后，翻页空白
              hidden: true,
              hasMore: false,
            });
          }
        }
      })
    },
    hideLoading() {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        loading: false
      });
    },
    loadMore() {
      this.showLoading();
      this.hideLoading();
    },
    
    onPullDownRefresh: function () {
      wx.stopPullDownRefresh()
    },
    /**
     * [onPullDownRefresh 下拉刷新数据]
     * @return {[type]} [description]
     */
    onPullDownRefresh() {
      wx.showNavigationBarLoading();
      wx.showToast({
        title: '数据更新',
        icon: 'success',
        duration: 2000
      })
      var that = this
      wx.request({
        url: getBookListUrl,
        data: { page: 1 },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data.data.list)
          that.setData({
            bookListItem: res.data.data.list
          })
          wx.hideNavigationBarLoading() //完成停止加载 
          wx.stopPullDownRefresh();
        }
      })

    },

    /**
     * [onReachBottom 上拉加载更多]
     * @return {[type]} [description]
     */
    onReachBottom() {
      this.loadMore();
    },
    onShareAppMessage: function (res) {
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
      }
      return {
        title: '微朗读-让孩子爱上读书',        
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }

});
